<template>
  <el-dialog
    :title="dataForm.op===0 ? $t('message.read') : $t('message.edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div slot="title" v-if="dataForm.op==0">{{$t('message.read')}}</div>
    <div slot="title" v-else-if="dataForm.op!=0">{{$t('message.edit')}}</div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <!-- <el-form-item :label="$t('message.userName')" prop="userName">
          <el-input v-model="dataForm.userName" :placeholder="$t('message.userName')" disabled></el-input>
      </el-form-item> -->
      <el-form-item :label="$t('message.email')" prop="email">
          <el-input v-model="dataForm.email" :placeholder="$t('message.email')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.phoneNo')" prop="phone">
          <el-input v-model="dataForm.phone" :placeholder="$t('message.phoneNo')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.remark')" prop="remarks">
          <el-input v-model="dataForm.remarks" :placeholder="$t('message.remark')" :disabled="this.dataForm.op==0"></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.serialNo')" prop="serialNo">
          <el-input v-model="dataForm.serialNo" :placeholder="$t('message.serialNo')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.dealer')" prop="dealerId">
          <el-select v-model="dataForm.dealerId" :disabled="this.dataForm.isAdmin==0">
            <el-option v-for="item in dataForm.dealList" :key="item.userId" :label="item.username" :value="item.userId" />
          </el-select>
      </el-form-item>
      <el-form-item :label="$t('message.usedTimes')" prop="useTimes">
          <el-input-number v-model="dataForm.useTimes" controls-position="right" :min="0" :label="$t('message.usedTimes')" disabled></el-input-number>
      </el-form-item>
      <el-form-item :label="$t('message.surplusTimes')" prop="remainTimes" :class="{ 'is-required': true }">
        <el-input-number v-model="dataForm.remainTimes" controls-position="right" :min="0" :label="$t('message.surplusTimes')" disabled></el-input-number>
      </el-form-item>
      <el-form-item :label="$t('message.status')" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status" :disabled="this.dataForm.op===0">
          <el-radio :label="0">{{$t('message.disable')}}</el-radio>
          <el-radio :label="1">{{$t('message.ok')}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('message.close')}}</el-button>
      <el-button v-show="dataForm.op===1" type="primary" @click="dataFormSubmit()">{{$t('message.submit')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogVisible: false,
        dataForm: {
          op: 0,
          id: 0,
          isAdmin: 0,
          remarks: '',
          phone: '',
          email: '',
          serialNo: '',
          phoneModelName: '',
          useTimes: '',
          remainTimes: '',
          status: 1,
          dealList: [],
          dealerId: ''
        },
        dataRule: {
          remainTimes: [
            { required: true, message: this.$t('message.noEmpty'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, op) {
        this.dataForm.id = id
        this.dataForm.op = op
        this.visible = true
        if (this.$refs['dataForm'] !== undefined) {
          this.$refs['dataForm'].resetFields()
        }
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/account/queryAccountInfo`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.id = data.account.id
              this.dataForm.remarks = data.account.remarks
              this.dataForm.phone = data.account.phone
              this.dataForm.email = data.account.email
              this.dataForm.serialNo = data.account.serialNo
              this.dataForm.phoneModelName = data.account.phoneModelName
              this.dataForm.useTimes = data.account.useTimes
              this.dataForm.remainTimes = data.account.remainTimes
              this.dataForm.status = data.account.status
              this.dataForm.dealerId = data.account.dealerId
              this.dataForm.dealList = data.dealList
              this.dataForm.isAdmin = data.isAdmin
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (!this.dataForm.remainTimes < 0) {
              this.$message.error(this.$t('message.mustGreater'))
            }
            this.$http({
              url: this.$http.adornUrl(`/sys/account/saveAccount`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'remarks': this.dataForm.remarks,
                'useTimes': this.dataForm.useTimes,
                'remainTimes': this.dataForm.remainTimes,
                'status': this.dataForm.status,
                'dealerId': this.dataForm.dealerId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('success'),
                  type: 'success',
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
